﻿package com.dsr.instances;

import java.util.Vector;
import com.dsr.util.enumu.NGramEnum;

public class DocumentInstancesBuilder {

	private Vector<DocumentNGrams> docNGramVec;

	private Vector<String> features;

	private DocumentInstances docInstances;

	private Vector<DocumentInfo> docInfoVec;
	
	private NGramEnum ngramType;
	
	private Vector<String> categoriesVec;
	
	public DocumentInstancesBuilder(Vector<DocumentInfo> docInfoVec, NGramEnum ngramType) {
		this.docInfoVec = docInfoVec;
		this.ngramType = ngramType;
		
	}
	
	private void buildFeatures() {
		if(features == null)
		{
			DocumentFeaturesBuilder dfb = new DocumentFeaturesBuilder(docNGramVec);
			dfb.buildFeatures();
			features = dfb.getFeaturesVec();
		}	
	}
	
	private void buildNGrams() {
		if(ngramType == null)
			ngramType = NGramEnum.WORD_UNIGRAM;
		DocumentNGramBuilder ngramBuilder = new DocumentNGramBuilder(docInfoVec,ngramType);
		ngramBuilder.build();
		docNGramVec = ngramBuilder.getDocNGramVec();
	}
	
	private void buildCategories()
	{
		categoriesVec = new Vector<String>();
		for(DocumentInfo doc: docInfoVec){
			if(!categoriesVec.contains(doc.getCategory()))
				categoriesVec.add(doc.getCategory());
		}
	}
	
	private void buildFeaturesValues()
	{
		DocumentValuesBuilder dvb = new DocumentValuesBuilder(features, docNGramVec);
		dvb.build();
		docInstances = new DocumentInstances(dvb.getDocumentInstances());
		docInstances.setCategoriesVec(categoriesVec);
		docInstances.setFeatures(features);
		docInstances.setFeaturesIDFVec(dvb.getFeaturesIDFValues());
	}
	
	public void buildInstances() {
		buildNGrams();
		buildFeatures();
		buildCategories();
		buildFeaturesValues();
	}

	public DocumentInstances getDocumentInstances() {
		return docInstances;
	}

	public Vector<DocumentNGrams> getDocNGramVec() {
		return docNGramVec;
	}

	public void setDocNGramVec(Vector<DocumentNGrams> docNGramVec) {
		this.docNGramVec = docNGramVec;
	}
	
	public Vector<DocumentInfo> getDocInfoVec() {
		return docInfoVec;
	}
	
	public void setDocVec(Vector<DocumentInfo> docInfoVec) {
		this.docInfoVec = docInfoVec;
	}

	public NGramEnum getNgramType() {
		return ngramType;
	}

	public void setNgramType(NGramEnum ngramType) {
		this.ngramType = ngramType;
	}

}
